package com.liang.maogouyuyin.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.liang.maogouyuyin.TheBaseApplication
import com.liang.maogouyuyin.databinding.FragmentAnimalBinding
import com.liang.maogouyuyin.ui.adapter.TrainingAdapter
import com.liang.maogouyuyin.ui.viewmodel.PetMenuViewModel

class AnimalFragment: Fragment() {
    private lateinit var binding: FragmentAnimalBinding

    private lateinit var petMenuViewModel: PetMenuViewModel

    private lateinit var adapter: TrainingAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentAnimalBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initData()
        initView()
        updateUI()
    }

    private fun initData() {
        petMenuViewModel = TheBaseApplication.getInstance().petMenuViewModel
        petMenuViewModel.getAnimalList()
    }

    private fun initView() {
        binding.animalRecyclerview.layoutManager = LinearLayoutManager(requireContext())
        adapter = TrainingAdapter(requireContext())
        adapter.setAnimal(true)
    }

    private fun updateUI() {
        petMenuViewModel.animalList.observe(viewLifecycleOwner) {
            adapter.setList(it)
            binding.animalRecyclerview.adapter = adapter
        }
    }
}